package com.example.zbh.dsliao.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.example.zbh.dsliao.R;
import com.example.zbh.dsliao.model.entity.NetSongList;
import com.example.zbh.dsliao.ui.activty.SongListActivity;
import com.example.zbh.dsliao.utils.MyEventBus;

import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by yaolin on 15/11/23.
 */
public class SongListAdapter extends RecyclerView.Adapter<SongListAdapter.MyViewHolder> {
    private List<NetSongList> datas;
    private Context context;
    private setOnclickListener listener;
    private int currentIndex;


    public interface setOnclickListener {
        void onItemClick(NetSongList data, int position);
    }

    public void setListener(setOnclickListener listener) {
        this.listener = listener;
    }

    /**
     * 构造方法
     **/
    public SongListAdapter(Context context) {
        this.context = context;
        //注册EventBus
        EventBus.getDefault().register(this);
    }


    public void unRegister(){
        EventBus.getDefault().unregister(this);
    }

    public void onEvent(MyEventBus.PlayerCurrentIndex playerCurrentIndex){
        this.currentIndex = playerCurrentIndex.getCurrentPosition();
        Log.d("SongListAdapter", "currentIndex:" + currentIndex);
        notifyDataSetChanged();
    }


    /**
     * 添加数据
     **/
    public void addData(List<NetSongList> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }





    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View v  = inflater.inflate(R.layout.list_song, null);
        return new MyViewHolder(v);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        if (datas != null && datas.size() > 0) {
            NetSongList e = datas.get(position);
            holder.music_artist.setText(e.getSingerName());
            holder.music_title.setText(e.getName());
            holder.music_position.setText(position + 1 + "");
            holder.position = position;
            if(  this.currentIndex  == position){
                holder.music_artist.setTextColor(Color.BLUE);
                holder.music_position.setTextColor(Color.BLUE);
                holder.music_title.setTextColor(Color.BLUE);
            }
        }
    }

    @Override
    public int getItemCount() {
        return datas != null && datas.size() > 0 ? datas.size() : 0;
    }


    class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private TextView music_title,music_artist, music_position;
        private ImageView downLoad_img;
        private LinearLayout linearLayout;

        private  int position;

        public MyViewHolder(View itemView) {
            super(itemView);
            music_title = (TextView) itemView.findViewById(R.id.music_title);
            music_artist = (TextView) itemView.findViewById(R.id.music_artist);
            music_position = (TextView)  itemView.findViewById(R.id.music_position);
            downLoad_img = (ImageView) itemView.findViewById(R.id.downLoad_img);
            downLoad_img.setOnClickListener(this);
            linearLayout = (LinearLayout) itemView.findViewById(R.id.linearLayout);
            linearLayout.setOnClickListener(this);
        }
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case  R.id.linearLayout:

                    if(datas != null && datas.size() > 0){
                        NetSongList e = datas.get(position);
                        if(listener != null){
                            listener.onItemClick(e, position);
                        }

                    }
                    break;
                case R.id.downLoad_img:
                    if (loaderClickListener != null ){
                        NetSongList netSongList = datas.get(getLayoutPosition());
                        if (netSongList != null){
                            loaderClickListener.OnDownLoaderClick(netSongList);
                        }
                    }
                    break;
            }
        }

        private void showPopupWindow(View view) {
            View contentView = LayoutInflater.from(context).inflate(R.layout.pop_window, null);

            PopupWindow popupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT, true);

          popupWindow.setTouchable(true);

            popupWindow.setTouchInterceptor(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {

                    return false;
                    // 这里如果返回true的话，touch事件将被拦截
                    // 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss
                }
            });

            // 如果不设置PopupWindow的背景，无论是点击外部区域还是Back键都无法dismiss弹框
            // 我觉得这里是API的一个bug
            popupWindow.setBackgroundDrawable(context.getResources().getDrawable(
                    R.drawable.shape_popupwindow));

            // 设置好参数之后再show
            popupWindow.showAsDropDown(view);

        }
    }


    //下载歌曲接口
    public interface  OnDownLoaderClickListener{
        void OnDownLoaderClick(NetSongList songList);
    }
    private OnDownLoaderClickListener loaderClickListener;
    public void setOnDownLoaderClickListener(OnDownLoaderClickListener loaderClickListener){
        this.loaderClickListener = loaderClickListener;
    }




}